﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace Department.App_Code
{
    public class Department
    {
        public int ID { get; set; }
        public String DeskNameBG { get; set; }
        public String DeskNameEN { get; set; }
    }

    public class DepartmentProperties
    {
        private static string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DepartmentConnectionString"].ConnectionString;

        public static Department GetDepartment(HttpRequest req)
        {
            return GetDepartment(req["dept"]);
        }

        /// <summary>
        /// Returns a department tuple by given department "web name"
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
		public static Department GetDepartment(string dept)
        {
            SqlConnection conn = new SqlConnection(strConnString);
     
            try
            {
                conn.Open();

                string pageSelect = "EXEC sp_GetDepartment @DepName";
                SqlCommand pgCmd = new SqlCommand(pageSelect, conn);
                pgCmd.Parameters.AddWithValue("@DepName", dept);
                var reader = pgCmd.ExecuteReader();
                reader.Read();
                return new Department()
                {
                    ID = reader.GetInt32(0),
                    DeskNameBG = reader.GetString(1),
                    DeskNameEN = reader.GetString(2)
                };
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

		/// <summary>
		/// Get the ID only. Required for backwards compatibility.
		/// </summary>
		/// <param name="dept"></param>
		/// <returns></returns>
		public static int GetDepartmentID(string dept)
		{
			return GetDepartment(dept).ID;
		}

		public static int GetDepartmentID(HttpRequest req)
		{
			return GetDepartment(req["dept"]).ID;
		}
    }
}